﻿/*******************************************************************************
* KindEditor - WYSIWYG HTML Editor for Internet
* Copyright (C) 2006-2011 kindsoft.net
*
* @author Roddy <luolonghao@gmail.com>
* @site http://www.kindsoft.net/
* @licence http://www.kindsoft.net/license.php
*******************************************************************************/

KindEditor.plugin('byblink', function (K) {
    var self = this, name = 'byblink'; cmd = self.cmd, range = cmd.range;
    self.plugin.link = {
        edit: function () {
            var lang = self.lang(name + '.'),
				html = '<div>' +
				        '<form id="addform" class="ke-upload-area ke-form" method="post" enctype="multipart/form-data" target="_blank" action="' + self.basePath + 'plugins/byblink/byblink.ashx">' +
				        '<div style="width:550px;margin:auto;">' +
                            '<label>' +
                                '<select name="dbselect" id="dbselect">' +
                                    '<option value="名人库">名人库</option>' +
                                    '<option value="名人聚合">名人聚合</option>' +
                                    '<option value="疾病库">疾病库</option>' +
                                    '<option value="穴位库">穴位库</option>' +
                                    '<option value="中药库">中药库</option>' +
                                    '<option value="按摩师库">按摩师库</option>' +
                                    '<option value="食品库">食品库</option>' +
                                    '<option value="营养素库">营养素库</option>' +
                                '</select>' +
                            '</label>' +
                            '<label>' +
                                 '<input type="text" name="keytxt" id="keytxt"/>' +
                            '</label>' +
                            '<input type="button" name="searchbtn" value="搜索" id="searchbtn"/>' +
                        '</div>' +
                        '</form>' +
                        '<div style="width:550px;margin:auto;">' +
                             '<hr style="width:550px;margin:auto;" />' +
                        '</div>' +
                        '<div id="resultdiv" name="resultdiv" style="width:550px;margin:auto;font-size:12px;display:block;overflow: hidden;height:280px;">' +
            //                                '<div style="width:150px;float:left;text-align:left;line-height:22px;"><a href="javascript:void(0)" >dddd</a></div>' +
            //                                '<div style="width:150px;float:left;text-align:left;line-height:22px;"></div>' +
            //                                '<div style="width:150px;float:left;text-align:left;line-height:22px;">dddd</div>' +
            //                                '<div style="width:150px;float:left;text-align:left;line-height:22px;">按摩摩师库</div>' +
            //                                '<div style="width:150px;float:left;text-align:left;line-height:22px;">dddd</div>' +
            //                                '<div style="width:150px;float:left;text-align:left;line-height:22px;">dddd</div>' +
                        '</div>' +
                        '<div style="border: medium none;clear: both;float: none;font-size: 0;height: 0;line-height: 0;"></div>' +
                        '<div style="width:550px;margin:auto;display:block;font-size:12px;">' +
                            'Url：<input class="ke-input-text" type="text" id="keUrl" name="url" style="width:260px;"/>' +
                        '</div></div>',
				dialog = self.createDialog({
				    name: name,
				    width: 600,
				    title: self.lang(name),
				    body: html,
				    noBtn: {
				        name: '取消',
				        click: function (e) {
				            keyTxt.val("");
				            self.hideDialog().focus();
				        }
				    },
				    yesBtn: {
				        name: self.lang('yes'),
				        click: function (e) {
				            var url = K.trim(urlBox.val());
				            if (url == 'http://' || K.invalidUrl(url)) {
				                alert(self.lang('invalidUrl'));
				                urlBox[0].focus();
				                return;
				            }
				            self.exec('createlink', url, '_blank').hideDialog().focus();
				        }
				    }
				}),
				div = dialog.div,
				urlBox = K('input[name="url"]', div);
            resultDiv = K('div[name="resultdiv"]', div);
            searchBtn = K('input[name="searchbtn"]', div);
            dbSelect = K('select[name="dbselect"]', div);
            keyTxt = K('input[name="keytxt"]', div);
            //keyTxt.val(self.cmd.selection().range.toString());
            var ff = self.cmd.selection().range.toString().indexOf(self.lang("specialword.left"));
            var ee = self.cmd.selection().range.toString().indexOf(self.lang("specialword.right"));
            if (ff >= 0 && ee >= 0) {
                keyTxt.val(self.cmd.selection().range.toString().substring(self.cmd.selection().range.toString().indexOf(self.lang("specialword.left")) + 1, self.cmd.selection().range.toString().indexOf(self.lang("specialword.right"))));
            }
            else keyTxt.val(self.cmd.selection().range.toString());
            urlBox.val('');
            self.cmd.selection();

            var a = self.plugin.getSelectedLink();
            if (a) {
                self.cmd.range.selectNode(a[0]);
                self.cmd.select();
                urlBox.val(a.attr('data-ke-src'));
            }
            urlBox[0].focus();
            urlBox[0].select();
            searchBtn.click(function () {
                dialog.showLoading(self.lang('ajaxLoading'));
                editor.sync();
                K.ajax(self.basePath + 'plugins/byblink/byblink.ashx?cs=' + escape(dbSelect.val()) + '&kw=' + escape(keyTxt.val()),
                function (data) {
                    dialog.hideLoading();
                    resultDiv.html("");
                    var i = 0;
                    var itemlist = new Array();
                    for (i = 0; i < data.length; i++) {
                        itemlist[i] = K('<div><input type="radio" value="' + data[i].link + '" name="mygroup">' + data[i].name + '</div>');
                        itemlist[i].click(function () {
                            urlBox.val(event.srcElement.value);
                           /* var pstr = resultDiv.html().split("<");
                            var j = 0;
                            for (j = 0; j < pstr.length; j++) {
                                if (pstr[j].indexOf("checked=") >= 0) {
                                    var startpos = pstr[j].indexOf("value=") + ("value=").length + 1;
                                    var subfind = pstr[j].substring(startpos);
                                    var endpos = subfind.indexOf("\"");
                                    //var endpos = pstr[j].indexOf("checked=") - 2;
                                    var rstr = pstr[j].substring(startpos, startpos + endpos);
                                    urlBox.val(rstr);
                                    break;
                                }
                            }*/
                        });
                        resultDiv.append(itemlist[i]);
                    }
                },
                 'GET', { aa: 1, bb: 2 }, 'json');
            });
        },
        'delete': function () {
            self.exec('unlink', null);
        }
    };
    self.clickToolbar(name, self.plugin.link.edit);
});
